﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
//using System.Xml.Linq;
using System.Data;
using Telerik.Web.UI;
using KPIS.Utils;
using KPIS.DBM;
using KPIS.GERP.GHRMS.IBLL;
using KPIS.GERP.GHRMS.BLL;
using KPIS.GERP.GHRMS.MODEL;

namespace KPIS.GERP.GHRMS.APP.App_UI.UserControls
{
    public partial class DC_Complains : System.Web.UI.UserControl
    {
        private SsMng ssMng = new SsMng();
        private IEmployeeTypeBLL empTypeBLL = new EmployeeTypeBLL();
        private IEmployeeBLL empBLL = new EmployeeBLL();
        private IJobSiteTypeBLL jobTypeBLL = new JobSiteTypeBLL();
        private DisciplinComplainBLL cmpBLL = new DisciplinComplainBLL();

        private object _dataItem = null;
        protected void Page_Load(object sender, EventArgs e)
        {
            
        }
        protected bool IsUserControlPostBack
        {
            get
            {
                return this.ViewState["IsUserControlPostBack"] != null;
            }
        }

        protected override void OnLoad(System.EventArgs e)
        {
            base.OnLoad(e);

            if (!IsUserControlPostBack)
            {
                this.ViewState.Add("IsUserControlPostBack", true);
            }
            if (DataItem != null)
            {
                ListComplainType();

                if (!(DataItem is Telerik.Web.UI.GridInsertionObject))
                {
                    txtCmpDate.SetValue(DataBinder.Eval(DataItem, "cmp_date", "{0:dd/MM/yyyy}").ToString());
                    cmbComplainType.SelectedValue = DataBinder.Eval(DataItem, "cmp_type_seq").ToString();

                    bool bInternal = DataBinder.Eval(DataItem, "cmp_emp_seq").ToString().Length > 0
                        && DataBinder.Eval(DataItem, "cmp_name").ToString().Length <= 0;
                    rdoInternal.Checked = bInternal; 
                    rdoExternal.Checked = !bInternal;
                    txtCmpEmployee.Enabled = rdoInternal.Checked;
                    btnFindCmpEmp.Visible = rdoInternal.Checked;
                    txtCmpName.Enabled = rdoExternal.Checked;
                    txtEmployee.Text = DataBinder.Eval(DataItem, "REC_NAME").ToString();
                    if (rdoInternal.Checked)
                    {
                        txtCmpEmployee.Text = DataBinder.Eval(DataItem, "CMP_TEXT").ToString();
                    }
                    else if (rdoExternal.Checked)
                    {
                        txtCmpName.Text = DataBinder.Eval(DataItem, "CMP_TEXT").ToString();
                    }

                    
                }
            }
           
        }
        public object DataItem
        {
            get
            {
                return this._dataItem;
            }
            set
            {
                this._dataItem = value;
            }
        }
        public void ListComplainType()
        {
            DataTable dt = cmpBLL.ListComplainTypes().Tables[0].Copy();

            cmbComplainType.DataSource = dt;
            cmbComplainType.DataBind();
        }
        protected void btnFindEmp_Click(object sender, ImageClickEventArgs e)
        {
            try
            {
                string st = HiddenEmployee.Value;
                if (st == "")
                    return;
                EmployeeInfo infoEmp = new EmployeeInfo();
                IList<EmployeeInfo> infoList = empBLL.ListEmployees(st);
                infoEmp = infoList[0];
                lblRecEmpSeq.Text = infoEmp.Sequence.ToString();
                txtEmployee.Text = infoEmp.Person.FullName;
            }
            catch
            {

            }
        }

        protected void btnFindEmp_Load(object sender, EventArgs e)
        {
            Session["ETID"] = 1;
            btnFindEmp.Attributes.Add("OnClick", "LinkMDdetail(" + HiddenEmployee.ClientID + "," + "'Forms/DialogEmp.aspx?ETID=" + Session["ETID"].ToString() + "'" + ",'../frmIframe.html',700,500)");
        }

        protected void btnAddnew_Click(object sender, EventArgs e)
        {
            cmpBLL.Insert(DateTime.Parse(txtCmpDate.GetStringValue())
                , Int32.Parse(cmbComplainType.SelectedValue)
                , lblCmpEmpSeq.Text.Length > 0 ? Int32.Parse(lblCmpEmpSeq.Text) : (int?)null
                , txtCmpName.Text.Trim()
                , txtDescr.Text.Trim()
                , lblRecEmpSeq.Text.Length > 0 ? Int32.Parse(lblRecEmpSeq.Text) : 0
                , txtRemarks.Text
                , ssMng.EmpSeqI);
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            cmpBLL.Update(Convert.ToInt32(lblSequence.Text.Trim())
                , DateTime.Parse(txtCmpDate.GetStringValue())
                , Int32.Parse(cmbComplainType.SelectedValue)
                , lblCmpEmpSeq.Text.Length > 0 ? Int32.Parse(lblCmpEmpSeq.Text) : (int?)null
                , txtCmpName.Text.Trim()
                , txtDescr.Text.Trim()
                , lblRecEmpSeq.Text.Length > 0 ? Int32.Parse(lblRecEmpSeq.Text) : 0
                , txtRemarks.Text
                , ssMng.EmpSeqI);
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {

        }

        protected void btnFindCmpEmp_Click(object sender, ImageClickEventArgs e)
        {
            try
            {
                string st = HidenCmpEmployee.Value;
                if (st == "")
                    return;
                EmployeeInfo infoEmp = new EmployeeInfo();
                IList<EmployeeInfo> infoList = empBLL.ListEmployees(st);
                infoEmp = infoList[0];
                lblCmpEmpSeq.Text = infoEmp.Sequence.ToString();
                txtCmpEmployee.Text = infoEmp.Person.FullName;
            }
            catch
            {

            }
        }
        protected void btnFindCmpEmp_Load(object sender, EventArgs e)
        {
            Session["ETID"] = 1;
            btnFindCmpEmp.Attributes.Add("OnClick", "LinkMDdetail(" + HidenCmpEmployee.ClientID + "," + "'Forms/DialogEmp.aspx?ETID=" + Session["ETID"].ToString() + "'" + ",'../frmIframe.html',700,500)");
        }

        protected void rdoInternal_CheckedChanged(object sender, EventArgs e)
        {
            txtCmpEmployee.Enabled = rdoInternal.Checked;
            btnFindCmpEmp.Visible = rdoInternal.Checked;
            txtCmpName.Enabled = rdoExternal.Checked;
        }

        protected void rdoExternal_CheckedChanged(object sender, EventArgs e)
        {
            txtCmpEmployee.Enabled = rdoInternal.Checked;
            btnFindCmpEmp.Visible = rdoInternal.Checked;
            txtCmpName.Enabled = rdoExternal.Checked;
        }

        protected void txtCmpEmployee_TextChanged(object sender, EventArgs e)
        {
            //
        }

        protected void txtEmployee_TextChanged(object sender, EventArgs e)
        {

        }

        protected void cmbEmployee_SelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
        {
            //
        }

        protected void txtRemarks_TextChanged(object sender, EventArgs e)
        {

        }

        protected void cmbComplainType_SelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
        {
          
        }
    }
}